#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#define EEXIST -1

int main(int args, char* argv[]){

	FILE *debug_info=fopen("debug_info", "w+");

#ifdef DEBUG
	fprintf(debug_info, "\n---------------start------------------\n");
	fprintf(debug_info, "Source (argv[1]): \t%s\n", argv[1]);
	fprintf(debug_info, "Destination (argv[2]): \t%s\n", argv[2]);
#endif

	int filesource = open(argv[1],O_RDONLY);
	if(filesource<0){
		return 1;
	}

#ifdef DEBUG
	fprintf(debug_info, "filesource: \t%d\n", filesource);
#endif

	int filedest = open(argv[2], O_CREAT | O_WRONLY | O_EXCL , 0666);
	if(filedest==EEXIST){
		fprintf(stderr, "File exists!\n");
		fprintf(debug_info, "File exists!\n");
		fprintf(debug_info, "\n---------------error------------------\n");

		return -1;
	}

#ifdef DEBUG
	fprintf(debug_info, "filedest: \t%d\n", filedest);
#endif

	char buffer[2048];
	int rd;
	int wr;

#ifdef DEBUG
	fprintf(debug_info, "buffer[1]:\t%d\n", buffer[1]);
#endif

	while(rd>0){
		rd = read(filesource, buffer, sizeof(buffer)); //read gibt die anzahlder gelesenen bytes zurück
		wr = write(filedest, buffer, rd); //so viele bytes wie gelesen wurden werden auch geschrieben
	}

	close(filesource);
	close(filedest);

#ifdef DEBUG
	fprintf(debug_info, "\n----------------end-------------------\n");
#endif
	return 0;
}
